package com.lvhx.springboot.callservice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class ServiceExecutorAwareBeanPostProcessor implements BeanPostProcessor{
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	private IServiceExecutor serviceExecutor;
	
	public void setServiceExecutor(IServiceExecutor serviceExecutor) {
		this.serviceExecutor = serviceExecutor;
	}

	@Override
	public Object postProcessAfterInitialization(Object obj, String s)
			throws BeansException {
		return obj;
	}

	@Override
	public Object postProcessBeforeInitialization(Object obj, String s)
			throws BeansException {
		if(obj instanceof ServiceExecutorAware){
			logger.debug("Invoking setServiceExecutor on ServiceExecutorAware bean'"+s+"'");
			if(serviceExecutor==null){
				logger.error(s + "'s serviceExecutor is null,pls set it correctly");
			}
			((ServiceExecutorAware)obj).setServiceExecutor(serviceExecutor);
		}
		return obj;
	}
}
